package com.sushi.utils;


import java.lang.reflect.Field;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import lombok.extern.slf4j.Slf4j;

/**
 * 用流的方法写代码
 * // TODO 请将此类置于项目model内，文件自动写在src.java下
 * @author Administrator
 */
@Slf4j
public class CodeUtil {


    private static String packageName;

    private static String fileName;

    /**
     * 写入所有内容 包含请求返回类
     * @param packageName 包名，如 com.example
     * @param fileName 定义的类名，如 Person
     * @param comment 类名备注，如 人员 (其他备注回自动加上)
     */
    public static void setAll(String packageName, String fileName, String comment) {
        setEntity(packageName, fileName, comment);
        setMapper(packageName, fileName, comment);
        setService(packageName, fileName, comment);
        setServiceImpl(packageName, fileName, comment);
        setController(packageName, fileName, comment);
        setRequest(packageName, fileName, comment);
        setResponse(packageName, fileName, comment);
    }

    /**
     * 写入一个 实体类
     * @param packageName 包名，如 com.example
     * @param fileName 定义的类名，如 Person
     * @param comment 类名备注，如 人员 (其他备注回自动加上)
     */
    public static void setEntity(String packageName, String fileName, String comment) {
        FileConfigContent fileConfigContent = new FileConfigContent();
        String nowType = "Entity";
        packageName = packageName.concat(".repository.entity");
        fileConfigContent.setPackageContent(packageName);
//        fileConfigContent.addImport(packageName.concat(".BaseEntity"));
        fileConfigContent.addImport("java.io.Serializable");
        fileConfigContent.addImport("com.baomidou.mybatisplus.annotation.TableField");
        fileConfigContent.addImport("com.baomidou.mybatisplus.annotation.TableId");
        fileConfigContent.addImport("com.baomidou.mybatisplus.annotation.TableName");
        fileConfigContent.addImport("lombok.Getter");
        fileConfigContent.addImport("lombok.Setter");
        fileConfigContent.addImport("lombok.ToString");
        fileConfigContent.setCommentContent(comment + " 实体类");
        fileConfigContent.addConfig("@Getter");
        fileConfigContent.addConfig("@Setter");
        fileConfigContent.addConfig("@ToString");
        fileConfigContent.addConfig("@TableName(\"`".concat(StringUtil.toUnderlineStyle(fileName)).concat("`\")"));
        fileConfigContent.setFileType(FileConfigContent.CLASS);
        fileConfigContent.setFileName(fileName.concat(nowType));
        fileConfigContent.setExtendsContent("BaseEntity<" + fileName.concat(nowType) + ">");
        fileConfigContent.addImplements("Serializable");
        fileConfigContent.addOtherContent("private static final long serialVersionUID = 1L");

        String fileContent = fileConfigContent.getFileContent();
        boolean result = IOUtil.writeSomething("src/main/java/" + packageName.replaceAll("\\.", "/"), fileName.concat(nowType), "java", fileContent);
        if (result) {
            log.info("{} {}：生成成功", fileName, "实体类");
        }else {
            log.info("{} {}：生成失败", fileName, "实体类");
        }
    }

    /**
     * 写入一个 数据层接口
     * @param packageName 包名，如 com.example
     * @param fileName 定义的类名，如 Person
     * @param comment 类名备注，如 人员 (其他备注回自动加上)
     */
    public static void setMapper(String packageName, String fileName, String comment) {
        FileConfigContent fileConfigContent = new FileConfigContent();
        String nowType = "Mapper";
        fileConfigContent.addImport(packageName.concat(".repository.entity.").concat(fileName + "Entity"));
        packageName = packageName.concat(".repository.mapper");
        fileConfigContent.setPackageContent(packageName);
        fileConfigContent.addImport("org.apache.ibatis.annotations.Mapper");
        fileConfigContent.addImport("com.baomidou.mybatisplus.core.mapper.BaseMapper");
        fileConfigContent.setCommentContent(comment + " 映射类");
        fileConfigContent.addConfig("@Mapper");
        fileConfigContent.setFileType(FileConfigContent.INTERFACE);
        fileConfigContent.setFileName(fileName.concat(nowType));
        fileConfigContent.setExtendsContent("BaseMapper<" + fileName.concat("Entity") + ">");

        String fileContent = fileConfigContent.getFileContent();
        boolean result = IOUtil.writeSomething("src/main/java/" + packageName.replaceAll("\\.", "/"), fileName.concat(nowType), "java", fileContent);
        if (result) {
            log.info("{} {}：生成成功", fileName, "映射类");
        }else {
            log.info("{} {}：生成失败", fileName, "映射类");
        }
    }

    /**
     * 写入一个 服务层接口
     * @param packageName 包名，如 com.example
     * @param fileName 定义的类名，如 Person
     * @param comment 类名备注，如 人员 (其他备注回自动加上)
     */
    public static void setService(String packageName, String fileName, String comment) {
        FileConfigContent fileConfigContent = new FileConfigContent();
        String nowType = "Service";
        fileConfigContent.addImport(packageName.concat(".repository.entity.").concat(fileName).concat("Entity"));
        fileConfigContent.addImport("com.baomidou.mybatisplus.extension.service.IService");
        String bigName = StringUtil.toCamelCaseStyle(fileName, true);
        fileConfigContent.addImport(packageName.concat(".response.").concat("ResponseBO"));
        fileConfigContent.addImport(packageName.concat(".response.").concat("ResponsePageBO"));
        fileConfigContent.addImport(packageName.concat(".response.").concat(bigName.concat("Resp")));
        fileConfigContent.addImport(packageName.concat(".request.").concat(bigName.concat("Req")));
        fileConfigContent.addImport(packageName.concat(".request.").concat(bigName.concat("PageReq")));

        packageName = packageName.concat(".service");
        fileConfigContent.setPackageContent(packageName);
        fileConfigContent.setCommentContent(comment + " 服务类");
        fileConfigContent.setFileType(FileConfigContent.INTERFACE);
        fileConfigContent.setFileName(fileName.concat(nowType));
        fileConfigContent.setExtendsContent("IService<" + fileName + "Entity>");

        //--------------------------------------- START    一般默认自带方法，不需要可直接注释 setOtherContent
        String defaultMethods =
                "\n\n    /**\n" +
                "     * 新增\n" +
                "     */\n" +
                "    public ResponseBO<" + bigName + "Resp> add(" + bigName + "Req req);\n" +
                "\n" +
                "    /**\n" +
                "     * 逻辑删除\n" +
                "     */\n" +
                "    public ResponseBO<" + bigName + "Resp> logicRemove(" + bigName + "Req req);\n" +
                "\n" +
                "    /**\n" +
                "     * 更新\n" +
                "     */\n" +
                "    public ResponseBO<" + bigName + "Resp> updateByField(" + bigName + "Req req);\n" +
                "\n" +
                "    /**\n" +
                "     * 列表\n" +
                "     */\n" +
                "    public ResponsePageBO<" + bigName + "Resp> listByCondition(" + bigName + "PageReq req);\n" +
                "\n" +
                "    /**\n" +
                "     * 详情\n" +
                "     */\n" +
                "    public ResponseBO<" + bigName + "Resp> detail(" + bigName + "Req req);\n\n"
                ;

        fileConfigContent.addOtherContent(defaultMethods);
        //--------------------------------------- END

        String fileContent = fileConfigContent.getFileContent();
        boolean result = IOUtil.writeSomething("src/main/java/" + packageName.replaceAll("\\.", "/"), fileName.concat(nowType), "java", fileContent);
        if (result) {
            log.info("{} {}：生成成功", fileName, "服务类");
        }else {
            log.info("{} {}：生成失败", fileName, "服务类");
        }
    }

    /**
     * 写入一个 服务 实现类
     * @param packageName 包名，如 com.example
     * @param fileName 定义的类名，如 Person
     * @param comment 类名备注，如 人员 (其他备注回自动加上)
     */
    public static void setServiceImpl(String packageName, String fileName, String comment) {
        FileConfigContent fileConfigContent = new FileConfigContent();
        String nowType = "ServiceImpl";
        fileConfigContent.addImport(packageName.concat(".repository.entity.").concat(fileName).concat("Entity"));
        fileConfigContent.addImport(packageName.concat(".repository.mapper.").concat(fileName).concat("Mapper"));
        fileConfigContent.addImport(packageName.concat(".service.").concat(fileName).concat("Service"));
        fileConfigContent.addImport("com.baomidou.mybatisplus.extension.service.impl.ServiceImpl");
        String bigName = StringUtil.toCamelCaseStyle(fileName, true);
        fileConfigContent.addImport(packageName.concat(".response.").concat("ResponseBO"));
        fileConfigContent.addImport(packageName.concat(".response.").concat("ResponsePageBO"));
        fileConfigContent.addImport(packageName.concat(".response.").concat(bigName.concat("Resp")));
        fileConfigContent.addImport(packageName.concat(".request.").concat(bigName.concat("Req")));
        fileConfigContent.addImport(packageName.concat(".request.").concat(bigName.concat("PageReq")));
        fileConfigContent.addImport(packageName.concat(".utils.ResponsePageUtil"));
        fileConfigContent.addImport(packageName.concat(".utils.ResponseUtil"));

        packageName = packageName.concat(".service.impl");
        fileConfigContent.setPackageContent(packageName);
        fileConfigContent.addImport("org.springframework.stereotype.Service");
        fileConfigContent.addImport("org.springframework.beans.factory.annotation.Autowired");
        fileConfigContent.addImport("lombok.extern.slf4j.Slf4j");
        fileConfigContent.setCommentContent(comment + " 服务实现类");
        fileConfigContent.addConfig("@Slf4j");
        fileConfigContent.addConfig("@Service");
        fileConfigContent.setFileType(FileConfigContent.CLASS);
        fileConfigContent.setFileName(fileName.concat(nowType));
        fileConfigContent.setExtendsContent("ServiceImpl<" + fileName + "Mapper, " + fileName + "Entity>");
        fileConfigContent.addImplements(fileName + "Service");
        fileConfigContent.addAutowired(fileName.concat("Mapper"));

        //--------------------------------------- START    一般默认自带方法，不需要可直接注释 setOtherContent

        String defaultMethods =
                "\n\n    /**\n" +
                "     * 新增\n" +
                "     */\n" +
                "    @Override\n" +
                "    public ResponseBO<" + bigName + "Resp> add(" + bigName + "Req req) {\n" +
                "        return ResponseUtil.ok();\n" +
                "    }\n" +
                "\n" +
                "    /**\n" +
                "     * 逻辑删除\n" +
                "     */\n" +
                "    @Override\n" +
                "    public ResponseBO<" + bigName + "Resp> logicRemove(" + bigName + "Req req) {\n" +
                "        return ResponseUtil.ok();\n" +
                "    }\n" +
                "\n" +
                "    /**\n" +
                "     * 更新\n" +
                "     */\n" +
                "    @Override\n" +
                "    public ResponseBO<" + bigName + "Resp> updateByField(" + bigName + "Req req) {\n" +
                "        return ResponseUtil.ok();\n" +
                "    }\n" +
                "\n" +
                "    /**\n" +
                "     * 列表\n" +
                "     */\n" +
                "    @Override\n" +
                "    public ResponsePageBO<" + bigName + "Resp> listByCondition(" + bigName + "PageReq req) {\n" +
                "        return ResponsePageUtil.ok();\n" +
                "    }\n" +
                "\n" +
                "    /**\n" +
                "     * 详情\n" +
                "     */\n" +
                "    @Override\n" +
                "    public ResponseBO<" + bigName + "Resp> detail(" + bigName + "Req req) {\n" +
                "        return ResponseUtil.ok();\n" +
                "    }"
                ;

        fileConfigContent.addOtherContent(defaultMethods);
        //--------------------------------------- END

        String fileContent = fileConfigContent.getFileContent();
        boolean result = IOUtil.writeSomething("src/main/java/" + packageName.replaceAll("\\.", "/"), fileName.concat(nowType), "java", fileContent);
        if (result) {
            log.info("{} {}：生成成功", fileName, "服务实现类");
        }else {
            log.info("{} {}：生成失败", fileName, "服务实现类");
        }
    }

    /**
     * 写入一个 控制类
     * @param packageName 包名，如 com.example
     * @param fileName 定义的类名，如 Person
     * @param comment 类名备注，如 人员 (其他备注回自动加上)
     */
    public static void setController(String packageName, String fileName, String comment) {
        FileConfigContent fileConfigContent = new FileConfigContent();
        String nowType = "Controller";
        fileConfigContent.addImport(packageName.concat(".service.").concat(fileName.concat("Service")));
        String littleName = StringUtil.toCamelCaseStyle(fileName, false);
        String bigName = StringUtil.toCamelCaseStyle(fileName, true);
        fileConfigContent.addImport(packageName.concat(".response.").concat("ResponseBO"));
        fileConfigContent.addImport(packageName.concat(".response.").concat("ResponsePageBO"));
        fileConfigContent.addImport(packageName.concat(".response.").concat(bigName.concat("Resp")));
        fileConfigContent.addImport(packageName.concat(".request.").concat(bigName.concat("Req")));
        fileConfigContent.addImport(packageName.concat(".request.").concat(bigName.concat("PageReq")));

        packageName = packageName.concat(".controller");
        fileConfigContent.setPackageContent(packageName);
//        fileConfigContent.addImport("org.springframework.beans.factory.annotation.Autowired");
        fileConfigContent.addImport("lombok.extern.slf4j.Slf4j");
        fileConfigContent.addImport("org.springframework.web.bind.annotation.*");
        fileConfigContent.addImport("org.springframework.beans.factory.annotation.*");
        fileConfigContent.setCommentContent(comment + " 控制器");
        fileConfigContent.addConfig("@RestController");
        fileConfigContent.addConfig("@Slf4j");
        fileConfigContent.setFileType(FileConfigContent.CLASS);
        fileConfigContent.setFileName(fileName.concat(nowType));
        fileConfigContent.addAutowired(fileName.concat("Service"));

        //--------------------------------------- START    一般默认自带方法，不需要可直接注释 setOtherContent
        String defaultMethods =
                "\n\n    /**\n" +
                "     * 新增\n" +
                "     */\n" +
                "    @PostMapping(\"/" + littleName + "/add\")\n" +
                "    public ResponseBO<" + bigName + "Resp> add(@RequestBody " + bigName + "Req req) {\n" +
                "        return " + littleName + "Service.add(req);\n" +
                "    }\n" +
                "\n" +
                "    /**\n" +
                "     * 逻辑删除\n" +
                "     */\n" +
                "    @PostMapping(\"/" + littleName + "/remove\")\n" +
                "    public ResponseBO<" + bigName + "Resp> remove(@RequestBody " + bigName + "Req req) {\n" +
                "        return " + littleName + "Service.logicRemove(req);\n" +
                "    }\n" +
                "\n" +
                "    /**\n" +
                "     * 更新\n" +
                "     */\n" +
                "    @PostMapping(\"/" + littleName + "/update\")\n" +
                "    public ResponseBO<" + bigName + "Resp> update(@RequestBody " + bigName + "Req req) {\n" +
                "        return " + littleName + "Service.updateByField(req);\n" +
                "    }\n" +
                "\n" +
                "    /**\n" +
                "     * 列表\n" +
                "     */\n" +
                "    @PostMapping(\"/" + littleName + "/list\")\n" +
                "    public ResponsePageBO<" + bigName + "Resp> list(@RequestBody " + bigName + "PageReq req) {\n" +
                "        return " + littleName + "Service.listByCondition(req);\n" +
                "    }\n" +
                "\n" +
                "    /**\n" +
                "     * 详情\n" +
                "     */\n" +
                "    @PostMapping(\"/" + littleName + "/detail\")\n" +
                "    public ResponseBO<" + bigName + "Resp> detail(@RequestBody " + bigName + "Req req) {\n" +
                "        return " + littleName + "Service.detail(req);\n" +
                "    }";

        fileConfigContent.addOtherContent(defaultMethods);
        //--------------------------------------- END

        String fileContent = fileConfigContent.getFileContent();
        boolean result = IOUtil.writeSomething("src/main/java/" + packageName.replaceAll("\\.", "/"), fileName.concat(nowType), "java", fileContent);
        if (result) {
            log.info("{} {}：生成成功", fileName, "控制器");
        }else {
            log.info("{} {}：生成失败", fileName, "控制器");
        }
    }

    /**
     * 写入一个 请求参数类
     * @param packageName 包名，如 com.example
     * @param fileName 定义的类名，如 Person
     * @param comment 类名备注，如 人员 (其他备注回自动加上)
     */
    public static void setRequest(String packageName, String fileName, String comment) {
        FileConfigContent fileConfigContent = new FileConfigContent();
        String nowType = "Req";
        packageName = packageName.concat(".request");
        fileConfigContent.setPackageContent(packageName);
        fileConfigContent.addImport("java.io.Serializable");
        fileConfigContent.addImport("lombok.ToString");
        fileConfigContent.addImport("lombok.Getter");
        fileConfigContent.addImport("lombok.Setter");
        fileConfigContent.setCommentContent(comment + " 请求参数类");
        fileConfigContent.addConfig("@Getter");
        fileConfigContent.addConfig("@Setter");
        fileConfigContent.addConfig("@ToString");
        fileConfigContent.setFileType(FileConfigContent.CLASS);
        fileConfigContent.setFileName(fileName.concat(nowType));
        fileConfigContent.setExtendsContent("RequestBO");
        fileConfigContent.addImplements("Serializable");
        fileConfigContent.addOtherContent("private static final long serialVersionUID = 1L");

        String fileContent = fileConfigContent.getFileContent();
        boolean result = IOUtil.writeSomething("src/main/java/" + packageName.replaceAll("\\.", "/"), fileName.concat(nowType), "java", fileContent);
        if (result) {
            log.info("{} {}：生成成功", fileName, "请求参数类");
        }else {
            log.info("{} {}：生成失败", fileName, "请求参数类");
        }
    }

    /**
     * 写入一个 分页请求参数类
     * @param packageName 包名，如 com.example
     * @param fileName 定义的类名，如 Person
     * @param comment 类名备注，如 人员 (其他备注回自动加上)
     */
    public static void setPageRequest(String packageName, String fileName, String comment) {
        FileConfigContent fileConfigContent = new FileConfigContent();
        String nowType = "PageReq";
        packageName = packageName.concat(".request");
        fileConfigContent.setPackageContent(packageName);
        fileConfigContent.addImport("java.io.Serializable");
        fileConfigContent.addImport("lombok.ToString");
        fileConfigContent.addImport("lombok.Getter");
        fileConfigContent.addImport("lombok.Setter");
        fileConfigContent.setCommentContent(comment + " 请求参数类 分页");
        fileConfigContent.addConfig("@Getter");
        fileConfigContent.addConfig("@Setter");
        fileConfigContent.addConfig("@ToString");
        fileConfigContent.setFileType(FileConfigContent.CLASS);
        fileConfigContent.setFileName(fileName.concat(nowType));
        fileConfigContent.setExtendsContent("RequestPageBO");
        fileConfigContent.addImplements("Serializable");
        fileConfigContent.addOtherContent("private static final long serialVersionUID = 1L");

        String fileContent = fileConfigContent.getFileContent();
        boolean result = IOUtil.writeSomething("src/main/java/" + packageName.replaceAll("\\.", "/"), fileName.concat(nowType), "java", fileContent);
        if (result) {
            log.info("{} {}：生成成功", fileName, "请求参数类");
        }else {
            log.info("{} {}：生成失败", fileName, "请求参数类");
        }
    }

    /**
     * 写入一个 返回参数类
     * @param packageName 包名，如 com.example
     * @param fileName 定义的类名，如 Person
     * @param comment 类名备注，如 人员 (其他备注回自动加上)
     */
    public static void setResponse(String packageName, String fileName, String comment) {
        FileConfigContent fileConfigContent = new FileConfigContent();
        String nowType = "Resp";
        packageName = packageName.concat(".response");
        fileConfigContent.setPackageContent(packageName);
        fileConfigContent.addImport("java.io.Serializable");
        fileConfigContent.addImport("lombok.ToString");
        fileConfigContent.addImport("lombok.Getter");
        fileConfigContent.addImport("lombok.Setter");
        fileConfigContent.setCommentContent(comment + " 返回参数类");
        fileConfigContent.addConfig("@Getter");
        fileConfigContent.addConfig("@Setter");
        fileConfigContent.addConfig("@ToString");
        fileConfigContent.setFileType(FileConfigContent.CLASS);
        fileConfigContent.setFileName(fileName.concat(nowType));
        fileConfigContent.addImplements("Serializable");
        fileConfigContent.addOtherContent("private static final long serialVersionUID = 1L");

        String fileContent = fileConfigContent.getFileContent();
        boolean result = IOUtil.writeSomething("src/main/java/" + packageName.replaceAll("\\.", "/"), fileName.concat(nowType), "java", fileContent);
        if (result) {
            log.info("{} {}：生成成功", fileName, "返回参数类");
        }else {
            log.info("{} {}：生成失败", fileName, "返回参数类");
        }
    }

    /**
     * 获取未处理过的建表sql语句
     * 需要自行修改表名，主键，备注
     * @param sourceClass 要转sql的类对象
     * @return 返回字符串(用于复制)
     */
    public static String getTableSql(Class<?> sourceClass) {
        return getTableSql(sourceClass, "database_name");
    }

    /**
     * 获取未处理过的建表sql语句
     * 需要自行修改主键，备注
     * @param sourceClass 要转sql的类对象
     * @param databaseName 自定义库名
     * @return 返回字符串(用于复制)
     */
    public static String getTableSql(Class<?> sourceClass, String databaseName) {
        return getTableSql(sourceClass, databaseName, StringUtil.toUnderlineStyle(sourceClass.getSimpleName().replace("Entity", "")));
    }

    /**
     * 获取未处理过的建表sql语句
     * 需要自行修改主键，备注
     * @param sourceClass 要转sql的类对象
     * @param databaseName 自定义库名
     * @param tableName 自定义表名
     * @return 返回字符串(用于复制)
     */
    public static String getTableSql(Class<?> sourceClass, String databaseName, String tableName) {
        return getTableSql(sourceClass, databaseName, tableName, "表备注");
    }

    /**
     * 获取未处理过的建表sql语句
     * 需要自行修改主键
     * @param sourceClass 要转sql的类对象
     * @param databaseName 自定义库名
     * @param tableName 自定义表名
     * @param comment 表格备注
     * @return 返回字符串(用于复制)
     */
    public static String getTableSql(Class<?> sourceClass, String databaseName, String tableName, String comment) {
        Field[] fields = sourceClass.getDeclaredFields();
        StringBuilder builder = new StringBuilder("CREATE TABLE `");
        builder.append(databaseName).append("`.`").append(tableName).append("` (\n");

        for (Field field : fields) {
            String fieldName;
            if (StringUtil.equals((fieldName = field.getName()), "serialVersionUID")) continue;
            StringBuilder line = new StringBuilder("    `");
            line.append(StringUtil.toUnderlineStyle(fieldName)).append("` ");
            Class<?> type = field.getType();
            if (type.equals(LocalDateTime.class)) {
                line.append("datetime ");
                line.append("DEFAULT NULL COMMENT '备注',\n");
            }
            else {
                line.append("varchar(32) ");
                line.append("DEFAULT NULL COMMENT '备注',\n");
            }
            builder.append(line);
        }
        builder.append(  "    `gmt_create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',\n"
                       + "    `creator_id` varchar(32) DEFAULT NULL COMMENT '创建人Id',\n"
                       + "    `creator_name` varchar(32) DEFAULT NULL COMMENT '创建人名称',\n"
                       + "    `gmt_modify_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',\n"
                       + "    `modifier_id` varchar(32) DEFAULT NULL COMMENT '修改人id',\n"
                       + "    `modifier_name` varchar(32) DEFAULT NULL COMMENT '修改人名称',\n"
                       + "    `deleted` bigint DEFAULT '0' COMMENT '逻辑删除标识 0-未删除, 其他值是已删除',\n");
        builder.append(  "    PRIMARY KEY (``) USING BTREE \n");
        builder.append(  ") ENGINE=InnoDB COMMENT='").append(comment).append("';");
        return builder.toString();
    }

    /**
     * 快捷规范化实体类
     * @param source 传入类似`private String id ;`的代码
     * @return 返回规范结果
     */
    public static String normalizeEntityFiled(String source) {
        if (StringUtil.isBlank(source)) { return ""; }
        String comment = "\n    /**\n     * \n     */\n    ";
        StringBuilder builder = new StringBuilder();
        String[] split = source.split("\n+");
        for (int i = 0; i < split.length; i++) {
            String temp = comment;
            if (i == 0) {
                temp = temp.concat("@TableId(\"`");
            }else {
                temp = temp.concat("@TableField(\"`");
            }
            String[] line = split[i].trim().split(" ");
            if (line.length != 3) { continue; }
            log.info("转换字段：{}", line[2]);
            String tableFiledName = StringUtil.toUnderlineStyle(line[2]);
            temp = temp.concat(tableFiledName).concat("`\")\n");
            builder.append(temp);
            builder.append(split[i]).append("\n");
        }
        return builder.toString();
    }



    public static String getUpdateModel(String className, List<String> field) {
        className = StringUtil.toCamelCaseStyle(className, true).replaceAll("Entity", "");
        String source = "        if (StringUtil.isNotBlank(req.get{}())) {\n" +
                "            entity.set{}(req.get{}());\n" +
                "        }\n";
        StringBuilder content = new StringBuilder("        if (ObjectUtil.isEmpty(req) || StringUtil.isBlank(req.getId())) {\n" +
                "            throw new BusinessException(\"必要参数不能为空\");\n" +
                "        }\n" +
                "        {}Entity entity = getById(req.getId());\n".replaceAll("\\{}", className) +
                "        if (ObjectUtil.isEmpty(entity)) {\n" +
                "            return ResponseUtil.error(\"信息不存在，无法更新\");\n" +
                "        }\n");

        for (String s : field) {
            content.append(source.replaceAll("\\{}", StringUtil.toCamelCaseStyle(s, true)));
        }

        content.append("        \n" + "        entity.updateById();\n" + "        log.info(\"更新成功：{}\", JSON.toJSONString(entity));\n")
                .append("        return ResponseUtil.ok(BeanUtil.convert(entity, {}Resp.class));\n".replaceAll("\\{}", className));

        return content.toString();
    }
    public static String getUpdateModel(Class<?> clazz) {
        Field[] fields = clazz.getDeclaredFields();
        List<String> fieldNameList = new ArrayList<>();
        for (Field f : fields) {
            fieldNameList.add(f.getName());
        }
        return getUpdateModel(clazz.getSimpleName(), fieldNameList);
    }



    public static String getQueryModel(Class<?> clazz) {
        String className = StringUtil.toCamelCaseStyle(clazz.getSimpleName(), true).replaceAll("Entity", "");
        String source = "        wrapper.eq(StringUtil.isNotBlank(req.get{}()), @@Entity::get{}, req.get{}());\n".replaceAll("@@", className);
        StringBuilder content = new StringBuilder("        if (ObjectUtil.isEmpty(req) || ObjectUtil.isEmpty(req.getPageNum()) || ObjectUtil.isEmpty(req.getPageSize())) {\n" +
                "            throw new BusinessException(\"必要参数不能为空\");\n" +
                "        }\n" +
                "        LambdaQueryWrapper<{}Entity> wrapper = new LambdaQueryWrapper<>();\n".replaceAll("\\{}", className) +
                "        wrapper.eq({}Entity::getDeleted, 0L);\n".replaceAll("\\{}", className));

        Field[] fields = clazz.getDeclaredFields();
        for (Field f : fields) {
            content.append(source.replaceAll("\\{}", StringUtil.toCamelCaseStyle(f.getName(), true)));
        }
        content.append("\n        Page<{}Entity> page = page(new Page<>(req.getPageNum(), req.getPageSize()), wrapper);\n".replaceAll("\\{}", className))
                .append("        if (ObjectUtil.isEmpty(page) || CollectionUtil.isEmpty(page.getRecords())) {\n")
                .append("            return ResponsePageUtil.ok(req, new ArrayList<>(), 0L);\n").append("        }\n")
                .append("        List<{}Resp> convert = BeanUtil.convert(page.getRecords(), {}Resp.class);\n".replaceAll("\\{}", className))
                .append("        return ResponsePageUtil.ok(req, convert, page.getTotal());");
        return content.toString();
    }

    public static String getRemoveModel(Class<?> clazz) {
        String className = StringUtil.toCamelCaseStyle(clazz.getSimpleName(), true).replaceAll("Entity", "");
        return "\n        if (StringUtil.isBlank(req.getId())) {\n" +
                "            throw new BusinessException(\"id不能为空\");\n" +
                "        }\n" +
                "        {}Entity entity = new {}Entity();\n".replaceAll("\\{}", className) +
                "        entity.setId(req.getId());\n" +
                "        entity.setDeleted(1L);\n" +
                "        entity.updateById();\n" +
                "        return ResponseUtil.ok(BeanUtil.convert(entity, {}Resp.class));\n".replaceAll("\\{}", className);
    }

    public static String getDetailModel(Class<?> clazz) {
        String className = StringUtil.toCamelCaseStyle(clazz.getSimpleName(), true).replaceAll("Entity", "");
        return "        if (ObjectUtil.isEmpty(req) || StringUtil.isBlank(req.getId())) {\n" +
                "            throw new BusinessException(\"必要参数不能为空\");\n" +
                "        }\n" +
                "        {}Entity entity = getById(req.getId());\n".replaceAll("\\{}", className) +
                "        if (ObjectUtil.isEmpty(entity)) {\n" +
                "            throw new BusinessException(\"对象不存在\");\n" +
                "        }\n" +
                "        return ResponseUtil.ok(BeanUtil.convert(entity, {}Resp.class));".replaceAll("\\{}", className);
    }

    public static String getAddModel(Class<?> clazz) {
        String className = StringUtil.toCamelCaseStyle(clazz.getSimpleName(), true).replaceAll("Entity", "");
        return "        {}Entity entity = BeanUtil.convert(req, {}Entity.class);\n".replaceAll("\\{}", className) +
                "        if (ObjectUtil.isEmpty(entity)) {\n" +
                "            throw new BusinessException(\"保存失败\");\n" +
                "        }\n" +
                "        entity.insert();\n" +
                "        return ResponseUtil.ok(BeanUtil.convert(entity, {}Resp.class));".replaceAll("\\{}", className);
    }


}
